const {
	getRandomId,
	encPwd,
} = require('../../../dbUtils')
const {
	getTime,
} = require('../../../dateUtils')
const {
	checkMongoIndex
} = require('./utils')

module.exports = async function initUser(cmsMongo, init = false) {
	await checkMongoIndex.call(cmsMongo, [{
		col: 'user',
		index: 'user_id',
	}], {
		unique: true
	})
	let userCol = await cmsMongo.getCollection('user')
	if (init === true)
		await userCol.deleteMany({})
	let user_id = '00000000'
	let user = await userCol.findOne({
		user_id
	})
	if (!user || !user._id)
		userCol.insertOne({
			user_id,
			username: 'admin',
			nickname: 'admin',
			pic: '',
			password: encPwd('123456'),
			orgpassword: '123456',
			mobile: '',
			mail: '',
			desc: 'Admin',
			role_ids: ['admin'],
			modify_time: getTime(),
		})
	if (init === true)
		userCol.insertOne({
			user_id: getRandomId(),
			username: 'test',
			nickname: 'test',
			pic: '',
			password: encPwd('123456'),
			orgpassword: '123456',
			mobile: '',
			mail: '',
			desc: 'Test',
			role_ids: ['test'],
			modify_time: getTime(),
		})
}